package cn.tedu.csmall.product.controller;

import cn.tedu.csmall.product.pojo.dto.AttributeTemplateAddNewDTO;
import cn.tedu.csmall.product.pojo.dto.AttributeTemplateUpdateDTO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateListItemVO;
import cn.tedu.csmall.product.pojo.vo.AttributeTemplateStandardVO;
import cn.tedu.csmall.product.service.IAttributeTemplateService;
import cn.tedu.csmall.product.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/attributeTemplates")
@Api(tags = "02. 属性模板管理模块")
@Validated
@Slf4j
public class AttributeTemplateController {
    @Autowired
    private IAttributeTemplateService service;

    // http://localhost:8080/attributeTemplate/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加属性模板")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid AttributeTemplateAddNewDTO attributeTemplateAddNewDTO) {
        log.debug("开始处理【添加属性模板】的业务,参数: {}",attributeTemplateAddNewDTO);
        service.addNew(attributeTemplateAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/attributeTemplate/9527/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除属性模板")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "属性模板ID",required = true,dataType = "long")
    })
    public JsonResult delete(@PathVariable @Range(min = 1,message = "请提交有效的ID值!") Long id) {
        log.debug("开始处理【根据ID删除属性模板】的请求，参数：{}", id);
        service.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/attributeTemplate/9527/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改属性模板")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "属性模板ID",required = true,dataType = "long")
    })
    public JsonResult updateInfo(@PathVariable @Range(min = 1,message = "请提交有效的ID值!") Long id,
                             @Valid AttributeTemplateUpdateDTO attributeTemplateUpdateDTO) {
        log.debug("开始处理【修改属性模板】的请求，ID：{}，新数据：{}", id, attributeTemplateUpdateDTO);
        service.updateInfo(id,attributeTemplateUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/attributeTemplates/9527
    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("根据ID查询属性模板详情")
    @ApiOperationSupport(order = 410)
    public JsonResult<AttributeTemplateStandardVO> getStandardById(
            @PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【根据ID查询属性模板详情】的请求，参数：{}", id);
        AttributeTemplateStandardVO queryResult = service.getStandardById(id);
        return JsonResult.ok(queryResult);
    }

    // http://localhost:9080/attributeTemplates
    @GetMapping("")
    @ApiOperation("查询属性模板列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AttributeTemplateListItemVO>> list() {
        log.debug("开始处理【属性模板列表】的业务,无参数");
        List<AttributeTemplateListItemVO> list = service.list();
        return JsonResult.ok(list);
    }
}
